package com.wholesmart.service.impl;

import java.util.List;
import java.util.Map;
import java.util.Set;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.wholesmart.common.bean.Page;
import com.wholesmart.mapper.BrowseAuthorityMapper;
import com.wholesmart.mapper.UserMapper;
import com.wholesmart.mapper.entity.User;
import com.wholesmart.service.UserService;

@Service
public class UserServiceImpl implements UserService {
	@Autowired
	private UserMapper userMapper;
	@Autowired
	private BrowseAuthorityMapper browseAuthoritymapper;

	@Override
	public Set<String> getAccessUrlsByUsername(String username) {
		User user = getByUsername(username);
		if (user != null) {
			return browseAuthoritymapper.selectAccessUrlsByUserId(user.getId());
		}
		return null;
	}

	@Override
	public Set<String> getFrontRoutesByUsername(String username) {
		User user = getByUsername(username);
		if (user != null) {
			return browseAuthoritymapper.selectFrontRoutesByUserId(user.getId());
		}
		return null;
	}

	@Override
	public void setUserStatusByUsername(String username, int status) {
		userMapper.updateUserStatusByUsername(username, status);
	}

	@Override
	public User getByUsername(String username) {
		return userMapper.selectByUsername(username);
	}

	@Override
	public User getById(Long id) {
		return userMapper.selectById(id);
	}

	@Override
	public Page<User> getPage(Map<String, Object> params) {
		Page<User> page = new Page<>();
		List<User> records = userMapper.selectPage(params);
		long total = userMapper.countForSelectPage(params);
		page.setRecords(records);
		page.setTotal(total);
		return page;
	}

	@Override
	public String save(User entity) {
		userMapper.insert(entity);
		return null;
	}

	@Override
	public void deleteById(Long id) {
		userMapper.deleteById(id);
	}

	@Override
	public List<User> getByDepertmentId(Long depertmentId) {
		return userMapper.selectByDepertmentId(depertmentId);
	}

	@Override
	public List<User> getByDepertmentIds(List<Long> departmentIds) {
		return userMapper.selectByDepertmentIds(departmentIds);
	}
}
